package com.promote.hotspot.server.netty.filter;

import com.promote.hotspot.server.netty.client.IClientEventListener;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.promote.hotspot.common.model.HotSpotMsg;
import org.promote.hotspot.common.model.MessageType;
import org.promote.hotspot.common.tool.NettyIpUtil;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 客户端上报自己的appName
 * 当消息类型为APP_NAME，代表client与server建立连接成功，
 * 然后调用IClientEventListener的newClient方法增加apps元数据信息
 *
 * @author enping.jep
 * @date 2023/11/16 11:20
 **/
@Component
@Order(2)
@Slf4j
public class AppNameFilter implements INettyMsgFilter {

    @Resource
    private IClientEventListener clientEventListener;

    @Override
    public boolean chain(HotSpotMsg message, ChannelHandlerContext ctx) {
        if (MessageType.APP_NAME == message.getMessageType()) {
            log.info("AppNameFilter receive message:" + message.toString());
            String appName = message.getAppName();
            //维护或者管理客户端的信息 - channel appname
            clientEventListener.addClient(appName, ctx);
            return false;
        }
        return true;
    }
}
